$(function() {
	$.ajax({
		url : serverURL + "category/getTopCategoryOfDatabase",
		type:"GET",
		success : function(data) {
			try {
				if (data.code == 0) {
					var html = "";
					data = data.data;
					$.each(data,function(i, k) {
						html += "<div class='box' style='width:170px;height:170px;float:left;margin-right:30px;'>" +
									"<div class='panel panel-default'><img alt='删除' src='resources/images/cancel.png'/>"+ 
									"<div class='panel-body'>" +
									"<i class='icon icon-3x icon-home'></i>"+
									"<input cid="+k.tcId+" value="+k.tcName +" style='border:0;text-align:left;width:120px;'>" +
									"</div></div></div>";
					});
					$('.box-list').empty().append(html);
					reBindEvent();
					showMajor();
				} else if (data.code != -1) {
					alert("数据加载失败!");
				}
			} catch (e) {
				alert("数据加载失败，请稍后重试" + e);
			}
		}		
	});
	
	/**------------修改一级分类------------*/
	function reBindEvent(){
		var oldContent=null;
		$('.box-list .box input').unbind("click").unbind("blur").unbind("keydown").bind("click",function(){
			oldContent=$(this).val();
			$(this).addClass("selected");
		}).bind("blur",function() {
			if($('.selected').val()==oldContent){
				return false;
			}else if ($('.selected').val() == "") {
				alert("分类名不能为空,请重新编辑！");
				$('.selected').val(oldContent);
				return false;
			}else{
				$.ajax({
					url : serverURL + "category/modifyTop",
					type : "POST",
					data : {
						"tcId" : $('.selected').attr("cid"),
						"sequence":"0",
						"tcName" : $('.selected').val()
					},
					success : function(data) {
						try {
							if (data.code == 0) {
								$('.selected').removeClass("selected");
//								alert("修改院校信息成功！");
							} else if (data.code != -1) {
								alert("数据加载失败!");
							}
						} catch (e) {
							alert("数据加载失败，请稍后重试" + e);
						}
					}
				});
			}
		}).bind("keydown",function(e){
			var $this = $(this);
			if (e.keyCode == 13) {
				$this.trigger("blur");
			}
		});
	}
	/**------------添加按钮功能------------*/
	$('.add_content').click(function(){
		var html="<div class='box' style='width:170px;height:170px;float:left;margin-right:30px;'><div class='panel panel-default'><img alt='删除' src='resources/images/cancel.png'/>"+
					"<div class='panel-body'><i class='icon icon-3x icon-home'></i></i><input type='text' style='border:0;text-align:left;width:120px;'/></div>" +
				"</div></div>";
		/**------------失去焦点/按下enter键触发事件------------*/
		$('.box-list').append(html).find(".box:last input").focus().bind("blur",function(){
			var $this=$(this);
			if($this.val()==""){
				$this.parents(".box").remove(); 
			}else{
				$.ajax({
					url : serverURL + "category/addTop",
					type:"POST",
					data : {"tcName":$(this).val(),"sequence":"0"},
					success : function(data) {
						try {
							if (data.code == 0) {
								$this.css({ "border": "0", "text-align": "left" }).unbind("blur").attr("cid",data.data); 
								alert("一级分类添加成功！");
								reBindEvent();
								showMajor();
							} else if (data.code != -1) {
								alert("数据加载失败!");
							}
						} catch (e) {
							alert("数据加载失败，请稍后重试" + e);
						}
					}		
				});
			}
		}).bind("keydown",function(e){
			var $this=$(this);
			if(e.keyCode==13){
				$this.trigger("blur");
			}
		});
	});
	/**-----------jquery动效插件--------------*/
	jQuery.fn.shake = function(intShakes, intDistance , intDuration ) {
	    this.each(function() {
	        var jqNode = $(this);
	        jqNode.css({position: 'relative'});
	        for (var x=1; x<=intShakes; x++) {
	            jqNode.animate({ left: (intDistance * -1) },(((intDuration / intShakes) / 4)))
	            .animate({ left: intDistance },((intDuration/intShakes)/2))
	            .animate({ left: 0 },(((intDuration/intShakes)/4)));
	        }
	    });
	    return this;
	}
	$(".del_content").click(function(){
		$('.box-list .box img').unbind();
		var $item=$(".box-list .box");
		if($item.length>0){
			var i=0;
			var s=setInterval(function(){
				var obj=$item[i];
				$(obj).find("img").show().focus().click(function(){
					delCollege(this);
				});
				$(obj).shake(3,3,350);
				if(i==($item.length-1)) clearInterval(s); i++;
			});
		}
		$("div:not(.box)").click(function(){
			$(".box-list .box").find("img").hide();
		});
		
	});
	
	/**------------删除分类------------*/
	function delCollege(object){
			var $this=$(object);
			var $collegeId=$this.next().find("input").attr("cid");
			var $collegeName=$this.next().find("input").val();
			if(confirm("是否删除所选分类？")){
				$.ajax({
					url : serverURL + "category/del",
					type : "GET",
					data : {
						"id" : $this.parent().find("input").attr("cid"),
						"type" : 1
					},
					success : function(data) {
						try {
							if (data.code == 0) {
								$this.parents(".box").remove(); 
								cancelImg();
							} else if (data.code != -1) {
								alert("数据加载失败!");
							}
						} catch (e) {
							alert("数据加载失败，请稍后重试" + e);
						}
					}
				});
			}else{
				cancelImg();
			}
	}
	
	/**------------删除时去除"取消键"------------*/
	function cancelImg(){
		var $item=$(".box-list .box");
		if($item.length>0){
			var i=$item.length;
			var s=setInterval(function(){
				var obj=$item[i];
				$(obj).find("img").hide();
				$(obj).shake(3,3,350).find("input").prop("checked",false);
				if(i==0) clearInterval(s); i--;
			});
		}
	}
	
	/**------------点击进入二级分类------------*/
	function showMajor(){
		$(".box-list .box .panel .panel-body i").bind("click",function(){
			$tcId=$(this).next().attr("cid");
			window.location.href="category/second.htm?tcId="+$tcId;
		});
	}
});